Populating Next Right Pointers in Each Node II

Follow up for problem "Populating Next Right Pointers in Each Node".

题目大意:与116题一致,但不是满二叉树

题目难度:Hard

import java.util.*;

/**
 * Created by gzdaijie on 16/6/9
 */
public class Solution {
    public void connect(TreeLinkNode root) {
        Queue<TreeLinkNode> queue = new LinkedList<>();
        if (root == null) return;

        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            TreeLinkNode pre = null, p;
            while (size-- > 0) {
                p = queue.poll();
                if (pre != null) pre.next = p;
                pre = p;
                if (p.left != null) queue.offer(p.left);
                if (p.right != null) queue.offer(p.right);
            }
        }
    }
}
gzdaijie            updated 2016-06-09 16:40:29

results matching ""

    No results matching ""